<template>
  <div class="stockReport">
    <jc-button
      :btn-arr="['reload', 'option']"
      :disab-arr="['close', 'cancel', 'using', 'com1', 'com2', 'freeze']"
      @refresh="handleGetUntreated"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
      >
        <el-table-column :label="$t('market.rkdh')" align="center" width="120">
          <template slot-scope="scope">
            <el-link type="primary" @click="pushDetail(scope.row)">
              {{ scope.row.finstockbillno }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.putStoageDate')" prop="finstockdate" align="center" width="100" />
        <el-table-column :label="$t('market.order')" prop="fsalebillno" align="center" width="100" />
        <el-table-column :label="$t('shop.customerOrder')" prop="sal_khddh" align="center" width="130" />
        <el-table-column :label="$t('market.prodept')" prop="fprddeptname" align="center" width="120" />
        <el-table-column :label="$t('market.productmolde')" prop="fmodel" align="center" width="120" />
        <el-table-column :label="$t('market.fmaterial')" align="center" width="130">
          <template slot-scope="scope">
            <el-link type="primary" @click="jumpMateriel(scope.row)">
              {{ scope.row.fmatfnumber }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('shop.fname2')" prop="fmatname" align="center" width="120" show-overflow-tooltip />
        <el-table-column :label="$t('shop.fspecificaTion')" prop="fmatspecification" align="center" width="140" show-overflow-tooltip />
        <el-table-column :label="$t('market.putInStorage')" prop="frealqty" align="center" width="100" />
        <el-table-column :label="$t('market.artificial')" align="center">
          <el-table-column :label="$t('market.artificialPrice')" prop="fmanplanunitprice" align="center" width="120" />
          <el-table-column :label="$t('market.artificialPrice2')" prop="fmanrealunitprice" align="center" width="120" />
          <el-table-column :label="$t('market.artificialAmount')" prop="fmanplanprice" align="center" width="120" />
          <el-table-column :label="$t('market.artificProportion')" prop="fmanplanpct" align="center" width="120" />
          <el-table-column :label="$t('market.artificialAmount2')" prop="fmanrealprice" align="center" width="120" />
          <el-table-column :label="$t('market.artificProportion2')" prop="fmanrealpct" align="center" width="120" />
          <el-table-column :label="$t('market.amountDiff')" prop="fdifmanprice" align="center" width="120" />
        </el-table-column>
        <el-table-column :label="$t('market.materials')" align="center">
          <el-table-column :label="$t('market.matplanunitprice')" prop="fmatplanunitprice" align="center" width="120" />
          <el-table-column :label="$t('market.matplanunitprice')" prop="fmatrealunitprice" align="center" width="120" />
          <el-table-column :label="$t('market.matplanprice')" prop="fmatplanprice" align="center" width="120" />
          <el-table-column :label="$t('market.matplanpct')" prop="fmatplanpct" align="center" width="120" />
          <el-table-column :label="$t('market.matrealprice')" prop="fmatrealprice" align="center" width="120" />
          <el-table-column :label="$t('market.matrealpct')" prop="fmatrealpct" align="center" width="120" />
          <el-table-column :label="$t('market.difmatprice')" prop="fdifmatprice" align="center" width="120" />
        </el-table-column>
        <el-table-column :label="$t('shop.subtotal')" align="center">
          <el-table-column :label="$t('market.planunitprice')" prop="fplanunitprice" align="center" width="90" />
          <el-table-column :label="$t('market.realunitprice')" prop="frealunitprice" align="center" width="90" />
          <el-table-column :label="$t('market.planprice')" prop="fplanprice" align="center" width="90" />
          <el-table-column :label="$t('market.realprice')" prop="frealprice" align="center" width="90" />
          <el-table-column :label="$t('market.difprice')" prop="fdifprice" align="center" width="90" />
        </el-table-column>
      </jc-table>
    </div>
    <!--    &lt;!&ndash;    分页器&ndash;&gt;-->
    <!--    <div class="footer">-->
    <!--      <jc-pagination-->
    <!--        :total="total"-->
    <!--        :page.sync="currentPage"-->
    <!--        :limit.sync="size"-->
    <!--        @pagination="handleGetUntreated"-->
    <!--      />-->
    <!--    </div>-->
  </div>
</template>

<script>
import {
  queryPrdInStockReportForm
} from '@/api/production/process'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { toMxBmina } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'ProOrderList',
  components: {
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 61,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      fbillno: '',
      tableHeader: [],
      tableHeader1: [],
      proData: []
    }
  },
  created() {
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 250px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val.map(item => {
            return toMxBmina(item)
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '分析列表')
        } else {
          this.sd = ''
          this.ed = ''
          this.detName = ''
          this.fbillno = ''
          this.sal_khddh = ''
          this.fnumber = ''
          this.searCollData.concat(this.details).forEach(res => {
            if (res.field === 'sd') {
              this.sd = res.seach
            } else if (res.field === 'ed') {
              this.ed = res.seach
            } else if (res.field === 'detName') {
              this.detName = res.seach
            } else if (res.field === 'fbillno') {
              this.fbillno = res.seach
            } else if (res.field === 'sal_khddh') {
              this.sal_khddh = res.seach
            } else if (res.field === 'fnumber') {
              this.fnumber = res.seach
            }
          })
          queryPrdInStockReportForm({
            detName: this.detName,
            fbillno: this.fbillno,
            sal_khddh: this.sal_khddh,
            fnumber: this.fnumber,
            sd: this.sd,
            ed: this.ed
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '分析列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取自造价格分析列表
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      this.sd = ''
      this.ed = ''
      this.detName = ''
      this.fbillno = ''
      this.sal_khddh = ''
      this.fnumber = ''
      this.searCollData.concat(this.details).forEach(res => {
        if (res.field === 'sd') {
          this.sd = res.seach
        } else if (res.field === 'ed') {
          this.ed = res.seach
        } else if (res.field === 'detName') {
          this.detName = res.seach
        } else if (res.field === 'fbillno') {
          this.fbillno = res.seach
        } else if (res.field === 'sal_khddh') {
          this.sal_khddh = res.seach
        } else if (res.field === 'fnumber') {
          this.fnumber = res.seach
        }
      })
      const DATA = {
        detName: this.detName,
        fbillno: this.fbillno,
        sal_khddh: this.sal_khddh,
        fnumber: this.fnumber,
        sd: this.sd,
        ed: this.ed
      }
      const RES = await queryPrdInStockReportForm(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.tableData.map(res => {
          res.fmanrealpct = res.fmanrealpct + '%'
          res.fmanplanpct = res.fmanplanpct + '%'
          res.fmatrealpct = res.fmatrealpct + '%'
          res.fmatplanpct = res.fmatplanpct + '%'
        })
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 详情
    pushDetail(ev) {
      const id = ev.fid
      this.$router.push({
        path: `/proStirageOrder/${id}`
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.stockReport {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
